const util = require('../../../../../../../../utils/util.js');
const api = require('../../../../../../../../config/api.js');
const user = require('../../../../../../../../services/user.js');

//获取应用实例
const app = getApp()
const openid = app.globalData.logininfo.openid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    createDialogShow: false,// 小组命名的弹出层
    userDialogShow: false,// 成员备注的弹出层
    userManagerShow: false,// 管理成员操作的底部弹出
    userIndex: '',// 成员管理的下标
    group: {},// 当前组的信息
    circleid: "",// 圈子id
    groupJoinUserList: [],// 用户信息
    searchText: "",//搜索的值
    groupName: '',// 分组名字
    isPlacedtopJoin: '',// 是否置顶
    updateGroupid: '',// 当前用户是否从本组删除：0表示移出
    userRemake: "",//当前用户的备注
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var circleid = options.circleid;
    var groupList = JSON.parse(options.groupList);
    var group = JSON.parse(options.group);
    _this.setData({
      group: group,
      circleid: circleid,
      groupList: groupList,
      groupName: group.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    _this.getgroupJoinUser();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 跳转到添加成员分组的页面
   */
  navigateToGroupUser: function () {
    var _this = this;
    var circleid = _this.data.circleid;
    var groupList = _this.data.groupList;
    var group = _this.data.group;
    wx.navigateTo({
      url: '../../group/groupUser/groupUser?circleid=' + circleid + '&groupList=' + JSON.stringify(groupList) + '&group=' + JSON.stringify(group)
    })
  },

  /**
   * 获取分组后的用户信息
   */
  getgroupJoinUser: function () {
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        data.circleid = _this.data.circleid;
        data.searchText = _this.data.searchText;
        data.groupid = _this.data.group.id;
        util.post(api.Home_GetgroupJoinUserApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              groupJoinUserList: res.result.list,
              ["group.count"]: res.result.count
            })
          }
        })
      })
    }
  },

  /**
     * 搜索
     */
  search: function (e) {
    var _this = this;
    var value = e.detail.value;
    _this.setData({
      searchText: value
    })
    _this.getgroupJoinUser();
  },

  /**
   * 分组命名弹窗
   */
  createDialogShow: function () {
    var _this = this;
    _this.setData({
      createDialogShow: true
    })
  },

  /**
   * 分组命名关闭弹窗
   */
  createDialogClose: function () {
    var _this = this;
    _this.setData({
      createDialogShow: false,
      groupName: _this.data.group.name
    })
  },

  /**
   * 分组命名提交
   */
  createDialogConfirm: function () {
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.groupid = _this.data.group.id;
        data.openid = openid;
        data.access_token = res.access_token;
        data.text = _this.data.groupName;
        data.circleid = _this.data.circleid;
        util.post(api.Home_SaveOrUpdateGroupApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              ["group.name"]: _this.data.groupName 
            })
            _this.createDialogClose();
          }
        })
      })
    }
  },

  /**
   * 分组命名的input输入事件
   */
  groupNameBindInput: function (e) {
    var _this = this;
    var groupName = e.detail.value;
    _this.setData({
      groupName: groupName
    })
  },

  /**
   * 成员管理的底部弹窗弹出
   */
  userManagerShow: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    _this.setData({
      userManagerShow: true,
      userIndex: index
    })
  },
  
  /**
   * 成员管理的底部弹窗关闭
   */
  userManagerClose: function () {
    var _this = this;
    _this.setData({
      userDialogShow: false,
      userManagerShow: false,
      userIndex: "",
      isPlacedtopJoin: '',
      updateGroupid: '',
      userRemake: ''
    })
  },

  /**
   * 成员置顶
   */
  userPlacedtop: function () {
    var _this = this;
    var isPlacedtopJoin = _this.data.groupJoinUserList[_this.data.userIndex].isPlacedtopJoin;
    _this.setData({
      isPlacedtopJoin: isPlacedtopJoin == 1 ? 0 : 1
    })
    _this.updateJoinUserInfo();
  },

  /**
   * 成员删除
   */
  userDelete: function () {
    var _this = this;
    _this.setData({
      updateGroupid: "0"
    })
    _this.updateJoinUserInfo();
  },

  /**
   * 成员备注弹窗打开
   */
  userDialogShow: function () {
    var _this = this;
    _this.setData({
      userDialogShow: true,
      userManagerShow: false
    })
  },

  /**
   * 成员备注弹窗关闭
   */
  userDialogClose: function () {
    var _this = this;
    _this.userManagerClose();
  },

  /**
   * 成员备注提交
   */
  userDialogConfirm: function(){
    var _this = this;
    _this.updateJoinUserInfo();
  },

  /**
   * 成员备注的input输入事件
   */
  userRemakeBindInput: function (e) {
    var _this = this;
    var userRemake = e.detail.value;
    _this.setData({
      userRemake: userRemake
    })
  },

  /**
   * 修改用户信息
   */
  updateJoinUserInfo: function () {
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.joinid = _this.data.groupJoinUserList[_this.data.userIndex].joinid;
        data.groupid = _this.data.updateGroupid;
        data.circleid = _this.data.circleid;
        data.isPlacedtop = _this.data.isPlacedtopJoin;
        data.text = _this.data.userRemake;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Home_UpdateJoinUserInfoApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.getgroupJoinUser();
            _this.userManagerClose();
          }
        })
      })
    }
  },

  /**
   * 跳转到添加组员日记页面
   */
  navigateToCircleinteractionByGroup: function () {
    var _this = this;
    var circleid = _this.data.circleid;
    var groupid = _this.data.group.id;
    wx.navigateTo({
      url: '../../../circleinteraction/circleinteraction?circleid=' + circleid + "&select_groupid=" + groupid
    })
  },

  /**
   * 跳转到添加组员日记页面
   */
  navigateToCircleinteractionByUser: function () {
    var _this = this;
    var circleid = _this.data.circleid;
    var openid = _this.data.groupJoinUserList[_this.data.userIndex].openid;
    wx.navigateTo({
      url: '../../../circleinteraction/circleinteraction?circleid=' + circleid + "&select_openid=" + openid
    })
  },

  /**
   * 进入用户主页
   */
  navigateToPersonhome: function (e) {
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '../../../../../personhome/personhome?openid=' + openid
    })
  },
})